package sz.szsmk.citizencard.ui.adapter;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.databinding.UserAddCardViewBinding;
import sz.szsmk.citizencard.model.network.bean.response.QueryUserCardResponse;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/7/16 11:04
 * description:我的添加卡片适配器
 */
public class UserAddCardAdapter extends RecyclerView.Adapter<UserAddCardAdapter.UserAddCardHolder> {

    private Context mCcntext;
    private List<QueryUserCardResponse.DataBean> mData;
    private DelArrressInterface delArrressInterface;
    private String mCard;

    public UserAddCardAdapter(Context context, List<QueryUserCardResponse.DataBean> data) {
        mCcntext = context;
        mData = data;
    }

    public void delSetOnClick(DelArrressInterface mdelArrressInterface) {
        delArrressInterface = mdelArrressInterface;
    }

    @Override
    public int getItemViewType(int position) {
        return R.layout.user_add_card_view;
    }

    @Override
    public UserAddCardHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        return UserAddCardHolder.creat(parent);
    }

    @Override
    public void onBindViewHolder(UserAddCardHolder holder, int position) {
        loadItemData(position, holder.binding);
    }

    /**
     * 定义删除、编辑及设置默认地址回调接口
     */
    public interface DelArrressInterface {
        public void onClick(View view, int position);
    }


    private void loadItemData(final int position, UserAddCardViewBinding binding) {
        if (mData != null) {
            QueryUserCardResponse.DataBean dataBean = mData.get(position);
            String cardNo = dataBean.getBindCardNo();
            if (!TextUtils.isEmpty(mCard) && mCard.equals(cardNo)) {
                binding.userAddBtn.setText("已添加");
                binding.userAddBtn.setEnabled(false);
            } else {
                binding.userAddBtn.setText("添加");
                binding.userAddBtn.setEnabled(true);
            }
            binding.userAddCardname.setText(dataBean.getCardTypeName());
            binding.userAddEndtime.setText(dataBean.getPackageTypeName()+"");
            binding.userAddCardnum.setText(dataBean.getCardNo());
            binding.userAddTypename.setText(dataBean.getCardNo());
            binding.userAddState.setText(dataBean.getStateName());



            StringBuilder str = new StringBuilder(dataBean.getBindCardNo().replace(" ", ""));

            int i = str.length() / 4;
            int j = str.length() % 4;

            for (int x = (j == 0 ? i - 1 : i); x > 0; x--) {
                str = str.insert(x * 4, " ");
            }

            binding.userAddCardnum.setText(str);
            binding.userAddTypename.setText(str);
//                if (dataBean.getCardState() != null && dataBean.getCardState().equals("1")) {
//                    binding.userAddState.setText("已激活");
//                }
            binding.userAddBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (delArrressInterface != null) {
                        delArrressInterface.onClick(v, position);
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return null == mData ? 0 : mData.size();
    }

    public void setCardNo(String cardNo) {
        mCard = cardNo;
    }

    public static class UserAddCardHolder extends RecyclerView.ViewHolder {

        private UserAddCardViewBinding binding;

        public static UserAddCardHolder creat(ViewGroup parent) {
            UserAddCardViewBinding binding = UserAddCardViewBinding
                    .inflate(LayoutInflater.from(parent.getContext()), parent, false);
            return new UserAddCardHolder(binding);
        }

        public UserAddCardHolder(UserAddCardViewBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }

}
